package cn.danger.com.model.action;

import android.content.Context;
import android.text.TextUtils;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.danger.com.model.listener.OnCommonListener;
import cn.danger.com.util.Check;
import cn.danger.com.util.Constants;
import cn.danger.com.util.MyNetManager;

/**
 * Created by 俊 on 2016/12/28.
 */
public class ForgetPasswordModelImpl implements ForgetPasswordModel {

    private Context context;

    public ForgetPasswordModelImpl(Context context) {
        this.context = context;
    }

    private String check(String phone, String check_code, String new_password) {
        if (TextUtils.isEmpty(phone)) {
            return Constants.PHONE_IS_EMPTY;
        }
        if (!Check.isMobileNO(phone)) {
            return Constants.PHONE_IS_ILLEGAL;
        }
        if (TextUtils.isEmpty(check_code)) {
            return Constants.CHECK_CODE_IS_EMPTY;
        }
        if (TextUtils.isEmpty(new_password)) {
            return Constants.PASSWORD_IS_EMPTY;
        }
        if (new_password.length() < 6) {
            return Constants.PASSWORD_IS_NO_LESS_THAN_SIX;
        }
        return Constants.IS_RIGHT;
    }

    @Override
    public void sendCheckCode(String phone, final OnCommonListener listener) {
        if (TextUtils.isEmpty(phone)) {
            listener.onError(Constants.PHONE_IS_EMPTY);
        }
        if (!Check.isMobileNO(phone)) {
            listener.onError(Constants.PHONE_IS_ILLEGAL);
        }
        if (!new MyNetManager(context).isNetworkAvailable()) {
            listener.onError(Constants.NETWORK_IS_NOT_AVAILABLE);
        } else {
            BmobSMS.requestSMSCode(phone, "Schedule", new QueryListener<Integer>() {
                @Override
                public void done(Integer integer, BmobException e) {
                    if (e == null) {
                        listener.onSuccess(null);
                    } else {
                        if (Check.checkByCode(e.getErrorCode()) == null) {
                            listener.onError(Constants.FAIL_TO_SEND_CHECK);
                        } else {
                            listener.onError(Check.checkByCode(e.getErrorCode()));
                        }
                    }
                }
            });
        }
    }

    @Override
    public void reset(String phone, String check_code, String new_password, final OnCommonListener listener) {
        String check_result = check(phone, check_code, new_password);
        if (check_result.equals(Constants.IS_RIGHT)) {
            if (new MyNetManager(context).isNetworkAvailable()) {
                BmobUser.resetPasswordBySMSCode(check_code, new_password, new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            listener.onSuccess(null);
                        } else {
                            if (Check.checkByCode(e.getErrorCode()) == null) {
                                listener.onError(Constants.FAIL_TO_RESET_PASSWORD);
                            } else {
                                listener.onError(Check.checkByCode(e.getErrorCode()));
                            }
                        }
                    }
                });
            } else {
                listener.onError(Constants.NETWORK_IS_NOT_AVAILABLE);
            }
        } else {
            listener.onError(check_result);
        }
    }
}
